package com.intelligentmeterreadingsys.waterlist.reviewdata.activity;

import java.util.List;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.povo.UserWaterVO;
import com.intelligentmeterreadingsys.waterlist.querywaterlist.activity.ListViewActivity;

import android.app.ActivityGroup;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

/**
 * 数据复查主界面
 * 
 * @author king
 * 
 */
public class ReviewDataMainActivity extends ActivityGroup {

	private RadioGroup group = null;
	private LinearLayout layout = null;
	private RadioButton radio1 = null;// 水表信息
	private RadioButton radio2 = null;// 水表列表信息
	private RadioButton radio3 = null;// 查询条件
	private List<UserWaterVO> list;
	public List<UserWaterVO> getList() {
		return list;
	}

	public void setList(List<UserWaterVO> list) {
		this.list = list;
	}

	private final static int DIALOG = 1;

	private String username;
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsernumber() {
		return usernumber;
	}

	public void setUsernumber(String usernumber) {
		this.usernumber = usernumber;
	}

	public String getBandingstatus() {
		return bandingstatus;
	}

	public void setBandingstatus(String bandingstatus) {
		this.bandingstatus = bandingstatus;
	}

	public String getCopystatus() {
		return copystatus;
	}

	public void setCopystatus(String copystatus) {
		this.copystatus = copystatus;
	}

	public String getWateradds() {
		return wateradds;
	}

	public void setWateradds(String wateradds) {
		this.wateradds = wateradds;
	}

	public String getWaterproperties() {
		return waterproperties;
	}

	public void setWaterproperties(String waterproperties) {
		this.waterproperties = waterproperties;
	}

	public String getWaterid() {
		return waterid;
	}

	public void setWaterid(String waterid) {
		this.waterid = waterid;
	}

	private String usernumber;
	private String bandingstatus;
	private String copystatus;
	private String wateradds;
	private String waterproperties;
	private String waterid;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.reviewwatermain);
		init();
	}

	public void init() {
		group = (RadioGroup) findViewById(R.id.reviewwater_info_radiogroup);
		layout = (LinearLayout) findViewById(R.id.reviewwater_layout);
		radio1 = (RadioButton) findViewById(R.id.reviewwater_add);
		radio2 = (RadioButton) findViewById(R.id.reviewwater_download);
		radio3 = (RadioButton) findViewById(R.id.reviewwater_onekeyupload);
		
		radio2.setChecked(false);
		group.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (radio1.getId() == checkedId) {
                        launchActivity("ReviewWaterinfoActivity", ReviewWaterinfoActivity.class, null);
				} else if (radio2.getId() == checkedId) {
                  
					launchActivity("ReviewListActivity", ReviewListActivity.class, null);
					 final ReviewListActivity act=(ReviewListActivity)getCurrentActivity();
					 
					
					ListView listview=act.getListView();
					act.flip();
					
					listview.setOnItemClickListener(new OnItemClickListener() {

						@Override
						public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
								long arg3) {
							
							 List<UserWaterVO> list=act.getList();
							if(list==null||list.size()<1){
								Toast.makeText(ReviewDataMainActivity.this, "没有复查数据", 1000).show();
								return;
							}
							 waterid=  list.get(arg2).getWaterid();
							  launchActivity("ReviewWaterinfoActivity", ReviewWaterinfoActivity.class, null);
							  radio1.setChecked(true);
						}
					});
				} else if (radio3.getId() == checkedId) {
                      launchActivity("ReviewQueryCondition", ReviewQueryCondition.class, null);
                      final ReviewQueryCondition set=(ReviewQueryCondition)getCurrentActivity();
                     Button button= set.button1;
                     
                     button.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							
							usernumber=set.edit1.getEditableText().toString();
							
							waterproperties=set.edit2.getEditableText().toString();
							copystatus=set.getUsertype();
							wateradds= set.edit4.getEditableText().toString();
							
							launchActivity("ReviewListActivity",
									ReviewListActivity.class, null);
							ReviewListActivity activity=(ReviewListActivity)getCurrentActivity();
							activity.queryDater();
							
							radio2.setChecked(true);
							
						}
					});
				}

			}
		});
		
		radio2.setChecked(true);
	}

	/**
	 * 界面切换
	 * 
	 * @param id
	 * @param activityClass
	 */
	public void launchActivity(String id, Class<?> activityClass, Bundle bundle) {
		layout.removeAllViews();

		Intent intent = new Intent(ReviewDataMainActivity.this, activityClass);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

		Window window = getLocalActivityManager().startActivity(id, intent);
		View view = window.getDecorView();
		layout.addView(view);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		switch (id) {
		case DIALOG:
			Builder builder = new android.app.AlertDialog.Builder(this);
			// 设置对话框的图标
			builder.setIcon(R.drawable.buttondraw);
			// 设置对话框的标题
			builder.setMessage("你还有未审核的数据！确定将下载新的数据原有的将会被删除！");

			// 添加一个确定按钮
			builder.setPositiveButton(" 确 定 ",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {

						}
					});

			builder.setNeutralButton("取消",
					new android.content.DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {

						}
					});
			// 创建一个单选按钮对话框
			dialog = builder.create();
			break;
		case DIALOG + 1:
			Builder builder1 = new android.app.AlertDialog.Builder(this);
			// 设置对话框的图标
			builder1.setIcon(R.drawable.buttondraw);
			builder1.setMessage("确定下载新审核？");
			builder1.setPositiveButton("确定",
					new android.content.DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {

						}
					});
			builder1.setNeutralButton("取消",
					new android.content.DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {

						}
					});
			// 创建一个单选按钮对话框
			dialog = builder1.create();
			break;
		}
		return dialog;
	}
	
	/**
	 * 打开子Activity的菜单事件
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_MENU){
			getCurrentActivity().openOptionsMenu(); 
		}
		return super.onKeyDown(keyCode, event);
	}
}
